package com.i2works.smartluolong.web.httpClient;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.i2works.smartluolong.web.entity.common.ClientData;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

public class HttpClient {



	DefaultHttpClient httpClient = new DefaultHttpClient();

   //JSON post
	public JSONObject  JSONPost(String url,JSONObject jsonParam) {
		// 第一步，创建HttpPost对象
		HttpPost httpPost = new HttpPost(url);
		
		JSONObject resJson = null;
		
		// 第二步，设置HTTP POST请求参数，  使用json对象
		try {
			StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8"); 		// 解决中文乱码问题
			entity.setContentEncoding("UTF-8");    
			entity.setContentType("application/json");    
			httpPost.setEntity(entity);
			    
			// 发起请求
			HttpResponse httpResponse = httpClient.execute(httpPost);  	
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {   
            	// 请求结束，返回结果。并解析json。  
            	String resData = EntityUtils.toString(httpResponse.getEntity());
            	resJson =JSONObject.parseObject(resData);
            }   
	    	// 省略了一堆的catch（上面不少函数需捕捉异常）
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
		//	httpClient.getConnectionManager().shutdown();
		}
		return resJson;
	}

	
	  /**
     * 发送 get请求
     */

    public  JSONObject Get(String url, Map<String, String> querys){
    	
    	JSONObject resJson = new JSONObject();
         try {
        	 HttpGet httpGet = new HttpGet(buildUrl(url, querys));
        	 httpGet.addHeader("Content-Encoding","UTF-8");
        	 httpGet.addHeader("Content-Type","application/json");
 			// 发起请求
 			HttpResponse httpResponse = httpClient.execute(httpGet);
             if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {   
             	// 请求结束，返回结果。并解析json。  
             	String resData = EntityUtils.toString(httpResponse.getEntity());
             	resJson =JSONObject.parseObject(resData);
             }   
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//	httpClient.getConnectionManager().shutdown();
		}
         
 		return  resJson;
    }

    public static String buildUrl(String url, Map<String, String> querys) throws UnsupportedEncodingException {
        StringBuilder sbUrl = new StringBuilder();
        sbUrl.append(url);
        if (null != querys) {
            StringBuilder sbQuery = new StringBuilder();
            for (Map.Entry<String, String> query : querys.entrySet()) {
                if (0 < sbQuery.length()) {
                    sbQuery.append("&");
                }
                if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
                    sbQuery.append(query.getValue());
                }
                if (!StringUtils.isBlank(query.getKey())) {
                    sbQuery.append(query.getKey());
                    if (!StringUtils.isBlank(query.getValue())) {
                        sbQuery.append("=");
                        sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
                    }
                }
            }
            if (0 < sbQuery.length()) {
                sbUrl.append("?").append(sbQuery);
            }
        }

        return sbUrl.toString();
    }

     public static void main(String[] args) {

  	   /* JSONObject  ServerData= result.getJSONObject("ServerData");
    		JSONArray arry=ServerData.getJSONArray("field");
    	         System.out.println(arry.toJSONString());*/
    	 ClientData  clientData=new ClientData();
    	 
     	  JSONObject  jsonObject=new 	 JSONObject();
  	  //用户认证
     	 /*      	  String url="http://112.124.6.116:4102/bipLYDZZZ/ws/user/user/getUserCertification";//用户认证
    	 Map<String, Object> map=new HashMap<String, Object>();
     	  map.put("accessKey", "ZDNWWVVqbERla00xWkVnd1RFUkZlRTFUZDNsTlJFVTBUSHBCTkV4NlFUSkpSRVV4VDJwVk1FOXFRVEU9");
		  map.put("scName", "洛阳");
		  map.put("loginName", "123");
		  map.put("userName", "123");*/
/*     	 clientData.setGUID("ZDNWWVVqbERla00xWkVnd1RFUkZlRTFUZDNsTlJFVTBUSHBCTkV4NlFUSkpSRVV4VDJwVk1FOXFRVEU9");
     	 clientData.setScName("洛阳");
     	 clientData.setLoginName("123");
     	clientData.setUserName("123");*/
     	   //证照库
   	       String url="http://47.95.117.223/bipLYDZZZ/ws/getType/getType/getLicenseType";//证照库
     	 	clientData.setGUID("ZDNWWVVqbERkM2xOUkVVMFRGUkJORXhVUVROSlJFVjNUMnBWTVU5cVRYcE1SRVY0VFZONlF6VmtT\\\\r\\\\nREE9");
    	 	clientData.setScName("洛阳");
    	 	clientData.setLoginName("123");
    	 	clientData.setUserName("123");
     	 
    	 	
/*     	  //证照查询接口
     	  String url="http://112.124.6.116:4102/bipLYDZZZ/ws/search/search/searchLicenses";//证照查询接口
    	  Map<String, Object> map=new HashMap<String, Object>();
    	  map.put("GUID", "ZDNWWVVqbERkM2xOUkVVMFRGUkJORXhVUVROSlJFVjNUMnBWTVU5cVRYcE1SRVY0VFZONlF6VmtT\\\\r\\\\nREE9");
		  map.put("scName", "洛阳");
		  map.put("loginName", "123");
		  map.put("userName", "123");
		  map.put("zzlx","河南省老年人优待证［人社］");
		  map.put("zzzzbh","38004988300000001");*/
    	 	
    	 	
    	 	
     	  
    	jsonObject.put("ClientData",clientData);
    //	jsonObject.put("ClientData",map);

    	//System.out.println(jsonObject.toJSONString());
    	HttpClient httpClient=new HttpClient();
   		JSONObject  result=httpClient.	JSONPost(url,jsonObject);
		System.out.println(result.toJSONString());
     }
     


 }